<template>
  <common-edit-dialog
    :title="dialogTitle"
    :visible.sync="dialogShow"
    :button-loading="loading"
    @cancel="close"
    @submit="submitForm"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="18">
        <el-col :span="24">
          <el-form-item label="工单标题" prop="workName">
            <common-input v-model="form.workName" placeholder="请输入数据源名"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="工单流程"
            prop="workProcessId"
            :style="warningOpen ? 'margin-bottom: 0' : 'margin-bottom:18px'"
          >
            <el-select
              v-model="form.workProcessId"
              placeholder="请选择"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in workProcessList"
                :key="index"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <div v-show="warningOpen" class="warning">
        <span class="el-icon-warning"
        >工单服务异常，工单流程数据暂时无法显示</span
        >
        </div>
        <el-col :span="24">
          <el-form-item label="备注">
            <common-input
              v-model="form.remark"
              placeholder="请输入备注"
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </common-edit-dialog>
</template>

<script>
  import {
    workOrder_saveWork,
    workOrder_WorkName,
    workOrder_WorkProcess
  } from '@/api/alarmCenter/index'

  export default {
    components: {},

    props: {},
    data() {
      return {
        warningOpen: false,
        dialogTitle: '工单生成', // 标题
        dialogShow: false, // 是否显示
        workProcessList: [],
        form: {
          remark: '',
          workProcessId: '',
          workName: '',
          alarmIds: []
        },
        rules: {
          workProcessId: [
            { required: true, message: '请选择处理流程', trigger: 'blur' }
          ],
          workName: [
            { required: true, message: '请获取工单标题', trigger: 'blur' }
          ]
        },
        loading: false
      }
    },
    watch: {},
    computed: {},
    methods: {
      /**
       * 关闭
       */
      close() {
        this.$refs.form.resetFields()
        this.dialogShow = false
        this.$emit('refresh')
      },
      /**
       * 显示
       */
      show(ids) {
        this.form.alarmIds = ids.split(',')
        workOrder_WorkName({
          ids: ids
        })
          .then((res) => {
            if (res && res.code === 200) {
              this.form.workName = res.data.workName
            } else {
              this.form. workName=''
            }
          })
        workOrder_WorkProcess({})
          .then((res) => {
            if (res && res.code === 200) {
              this.workProcessList = res.data
              if (this.workProcessList.length === 0) {
                this.warningOpen = true
              } else {
                this.warningOpen = false
              }
            } else {
              this.workProcessList =[]
            }
          })
        this.dialogShow = true
      },
      /**
       * 提交
       */
      submitForm() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true;
            const data = { ...this.form }
            workOrder_saveWork(data)
              .then((res) => {
                if (res && res.code === 200) {
                  this.close()
                  this.$GlobalSuccessMessage('生成成功')
                } else {
                  this.$GlobalErrorConfirm('生成失败', res.msg)
                }
              }).finally(()=>{
                this.loading = false;
              })
          }
        })
      }
    },
    created() {
    },
    mounted() {
    }
  }
</script>
<style lang="scss" scoped>
  .warning {
    margin-bottom: 4px;
  }

  .el-icon-warning {
    color: #f56c6c;
    margin-left: 100px;
    font-size: 12px;
    line-height: 16px;
  }
</style>
